package com.amumu.netctoss.action;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.amumu.netctoss.dao.AccountDao;
import com.amumu.netctoss.entity.Account;

/**
 * 账号
 */
@Controller
@Scope("prototype")
public class AccountAction extends BaseAction {
	@Resource
	private AccountDao dao;
	private List<Account> accountList;
	private Account act;
	private Map<String, Object> statusOptions;
	private Map<String, Object> occupationOptions;
	private boolean modifypwd;
	private String oldpwd;
	private String newpwd;
	private String status;
	private String idcardNo;
	private String loginName;
	// 查询条件
	private String shstatus;
	private String shidcardNo;
	private String shrealName;
	private String shloginName;

	// 查询列表
	public String findAll() {
		try {
			statusOptions = dao.getStatusOptions();

			if (shrealName != null)
				shrealName = new String(shrealName.getBytes("iso8859-1"), "utf-8");
			setPageCount(dao.getPageCount(PAGE_SIZE, shidcardNo, shrealName, shloginName, shstatus));
			accountList = dao.search(page, PAGE_SIZE, shidcardNo, shrealName, shloginName, shstatus);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//添加
	public String save() {
		try {
			if (idcardNo != null && idcardNo.length() > 0) {
				Account a = dao.findByIdcard(idcardNo);
				if (a != null)
					act.setRecommenderId(a.getId());
			}
			dao.save(act);
			ok = true;
		} catch (SQLIntegrityConstraintViolationException e) {
			ok = false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//查看详情
	public String detail() {
		try {
			statusOptions = dao.getStatusOptions();
			act = dao.findById(id);
			if (act.getRecommenderId() != null)
				idcardNo = dao.findIdcardNo(act.getRecommenderId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//转到更新
	public String toUpdate() {
		try {
			occupationOptions = dao.getOccupationOptions();
			act = dao.findById(id);
			if (act.getRecommenderId() != null)
				idcardNo = dao.findIdcardNo(act.getRecommenderId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	// 更新
	public String update() {
		try {
			String pwd = dao.findPassword(act.getId());
			if (modifypwd) {
				if (oldpwd.equals(pwd)) {
					act.setLoginPassword(newpwd);
				} else {
					ok = false;
					return SUCCESS;
				}
			} else {
				act.setLoginPassword(pwd);
			}
			if (idcardNo != null && idcardNo.length() > 0) {
				Account a = dao.findByIdcard(idcardNo);
				if (a != null)
					act.setRecommenderId(a.getId());
			}
			dao.update(act);
			ok = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//删除
	public String delete() {
		try {
			dao.delete(id);
			ok = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//更新状态
	public String state() {
		try {
			dao.setStatus(id, status);
			ok = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//验证账号名称唯一性
	public String verifyName() {
		try {
			if (act != null && !dao.hasLoginName(act.getLoginName())) {
				ok = true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	//检查账号名存在
	public String validName() {
		try {
			if (dao.hasLoginName(loginName)) {
				ok = true;
			} else {
				ok = false;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

    //根据身分证号获得账户
	public String findByIdcard() {
		try {
			Account a = dao.findByIdcard(idcardNo);
			if (a != null) {
				id = a.getId();
				loginName = a.getLoginName();
				ok = true;
			} else {
				ok = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	// getter and setter
	public List<Account> getAccountList() {
		return accountList;
	}

	public Account getAct() {
		return act;
	}

	public void setAct(Account act) {
		this.act = act;
	}

	public Map<String, Object> getStatusOptions() {
		return statusOptions;
	}

	public void setModifypwd(boolean modifypwd) {
		this.modifypwd = modifypwd;
	}

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Map<String, Object> getOccupationOptions() {
		return occupationOptions;
	}

	public String getIdcardNo() {
		return idcardNo;
	}

	public void setIdcardNo(String idcardNo) {
		this.idcardNo = idcardNo.trim();

	}

	public String getStatus() {
		return status;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName.trim();

	}

	public boolean isOk() {
		return ok;
	}

	public int getId() {
		return id;
	}

	public String getShstatus() {
		return shstatus;
	}

	public void setShstatus(String shstatus) {
		this.shstatus = shstatus;
	}

	public String getShidcardNo() {
		return shidcardNo;
	}

	public void setShidcardNo(String shidcardNo) {
		this.shidcardNo = shidcardNo;
	}

	public String getShrealName() {
		return shrealName;
	}

	public void setShrealName(String shrealName) {
		this.shrealName = shrealName;
	}

	public String getShloginName() {
		return shloginName;
	}

	public void setShloginName(String shloginName) {
		this.shloginName = shloginName;
	}

}
